package com.chujian.framework.starter.async;

import com.chujian.framework.util.ErvUs;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

/**
 * 定时调度器
 */
@Configuration
@EnableScheduling
public class ScheduleTaskConfig {

    @Bean
    public TaskScheduler taskScheduler() {

        int cpuProcessors = ErvUs.getCpuProcessors();
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();

        scheduler.setPoolSize(cpuProcessors);
        scheduler.setThreadNamePrefix("TaskScheduler-");
        scheduler.setWaitForTasksToCompleteOnShutdown(true);
        scheduler.setAwaitTerminationSeconds(60);

        scheduler.initialize();

        return scheduler;
    }

}
